package com.ruoyi.wzgl.ckgl.hjgl.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.wzgl.ckgl.hjgl.domain.Hjgl;
import com.ruoyi.wzgl.ckgl.hjgl.service.IHjglService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 货架管理Controller
 *
 * @author huacai
 * @date 2024-09-18
 */
@RestController
@RequestMapping("/wzgl/hjgl")
public class HjglController extends BaseController {
    @Resource
    private IHjglService hjglService;

    /**
     * 查询货架管理列表
     */
//    @PreAuthorize("@ss.hasPermi('wzgl:hjgl:list')")
    @GetMapping("/list")
    public TableDataInfo list(Hjgl hjgl) {
        startPage();
        List<Hjgl> list = hjglService.selectHjglList(hjgl);
        return getDataTable(list);
    }

    /**
     * 导出货架管理列表
     */
//    @PreAuthorize("@ss.hasPermi('wzgl:hjgl:export')")
    @Log(title = "货架管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Hjgl hjgl) {
        List<Hjgl> list = hjglService.selectHjglList(hjgl);
        ExcelUtil<Hjgl> util = new ExcelUtil<Hjgl>(Hjgl.class);
        util.exportExcel(response, list, "货架管理数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Hjgl> util = new ExcelUtil<Hjgl>(Hjgl.class);
        util.importTemplateExcel(response, "货架管理数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "货架管理", businessType = BusinessType.IMPORT)
//    @PreAuthorize("@ss.hasPermi('wzgl:hjgl:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Hjgl> util = new ExcelUtil<Hjgl>(Hjgl.class);
        InputStream inputStream = file.getInputStream();
        List<Hjgl> list = util.importExcel(inputStream);
        inputStream.close();
        int count = hjglService.batchInsertHjgl(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取货架管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('wzgl:hjgl:query')")
    @GetMapping(value = "/{hjglId}")
    public AjaxResult getInfo(@PathVariable("hjglId") Integer hjglId) {
        return success(hjglService.selectHjglByHjglId(hjglId));
    }

    /**
     * 新增货架管理
     */
//    @PreAuthorize("@ss.hasPermi('wzgl:hjgl:add')")
    @Log(title = "货架管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Hjgl hjgl) {
        hjgl.setUserId(Math.toIntExact(getUserId())); //用户ID
        hjgl.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        hjgl.setCreateBy(getUsername()); //用户名
        return toAjax(hjglService.insertHjgl(hjgl));
    }

    /**
     * 修改货架管理
     */
//    @PreAuthorize("@ss.hasPermi('wzgl:hjgl:edit')")
    @Log(title = "货架管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Hjgl hjgl) {
        return toAjax(hjglService.updateHjgl(hjgl));
    }

    /**
     * 删除货架管理
     */
//    @PreAuthorize("@ss.hasPermi('wzgl:hjgl:remove')")
    @Log(title = "货架管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{hjglIds}")
    public AjaxResult remove(@PathVariable Integer[] hjglIds) {
        return toAjax(hjglService.deleteHjglByHjglIds(hjglIds));
    }
}
